<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/autoload.php");

use JMessage\JMessage;
use JMessage\IM\User;

class control {

    function __construct($appkey, $masterkey) {
        $this->jpush = new \JPush\Client($appkey, $masterkey);
        $this->jmessage = new JMessage($appkey, $masterkey, ['disable_ssl' => true]);
        $this->user = new User($this->jmessage);
    }

    function pushmsg($title, $msg, $rid, $extras) {
        $push = $this->jpush->push();
        $cid = $push->getCid($count = 1, $type = 'push');
        $mycid = $cid['body']['cidlist'][0];
        $platform = array('android');
        $android_notification = array(
            'title' => $title,
            'builder_id' => 2,
            'extras' => $extras
        );
        $response = $push->setCid($mycid)
                ->setPlatform($platform)
                ->addRegistrationId($rid)
                ->androidNotification($msg, $android_notification)
                ->send();
        return true;
//        try {
//            $response = $push->setCid($mycid)
//                    ->setPlatform($platform)
//                    ->addRegistrationId($rid)
//                    ->androidNotification($msg, $android_notification)
//                    ->send();
//            var_dump($response);
//            return true;
//        } catch (\JPush\Exceptions\JPushException $e) {
//            return false;
//        }
    }

    function rgimaccount($username, $pwd, $nickname) {
        $res = $this->user->register($username, $pwd);
        $res1 = $this->user->update($username, array("nickname" => $nickname));
        return true;
    }

}
